package org.cshoo.sys.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.ResultEvent;
	
	import org.cshoo.common.util.MessageUtil;
	import org.cshoo.sys.business.OrganizationDelegate;
	import org.cshoo.sys.event.UpdateOrganizationEvent;
	import org.cshoo.sys.model.vo.Organization;
	
	public class UpdateOrganizationCommand implements ICommand, IResponder
	{
		public var organization:Organization;
		public var organizationCopy:Organization;
		
		public function execute(event:CairngormEvent):void
		{
			organization = (event as UpdateOrganizationEvent).organization;
			organizationCopy = (event as UpdateOrganizationEvent).organizationCopy;
			var organizationDelegate:OrganizationDelegate = new OrganizationDelegate(this);
			organizationDelegate.updateOrganization(organizationCopy); 
		}
		
		public function result(event:Object):void
		{
			if((event as ResultEvent).result as Boolean)
			{
			    organization.code = organizationCopy.code;
			    organization.name = organizationCopy.name;
				organization.level = organizationCopy.level;
				organization.description = organizationCopy.description;
				
				if(organization.parent != organizationCopy.parent || organization.order != organizationCopy.order)
				{
					organization.parent.children.removeItemAt(organization.parent.children.getItemIndex(organization));
					organization.parent = organizationCopy.parent;
					organization.order = organizationCopy.order;
					if((organization.order - 1) < organization.parent.children.length)
						organization.parent.children.addItemAt(organization,organization.order - 1);
					else
						organization.parent.children.addItemAt(organization,organization.parent.children.length);
				}
				
				MessageUtil.showInfoMessage("组织机构数据修改成功");
			}
			else
				MessageUtil.showErrorMessage("组织机构数据修改失败");
		}
		
		public function fault(info:Object):void
		{
			Alert.show("服务器端已经停止");
		}
	}
}